package com.suning.sawp.service.impl.promotion;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.suning.sawp.constants.CacheKeyConstants;
import com.suning.sawp.constants.ExchangeConstants;
import com.suning.sawp.dto.exchange.CityInfoDto;
import com.suning.sawp.intf.promotion.CityTransService;
import com.suning.sawp.po.promotion.CityTrans;
import com.suning.sawp.service.util.RedisCacheUtils;

/**
 * 
 * 城市转码实现类<br>
 * 〈功能详细描述〉
 *
 * @author Wuxiao
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
@Service
public class CityTransServiceImpl implements CityTransService {

    @Autowired
    RedisCacheUtils cacheUtils;

    @Resource
    MongoTemplate mongoTemplate;

    @Override
    public String getCommerceFromMDMCode(String commerceCode) {

        String mdmCityCode = cacheUtils.hget(CacheKeyConstants.COMMERCE_CITY_CODE_TO_MDM_CODE, commerceCode,
                String.class);
        if (StringUtils.isBlank(mdmCityCode)) {
            Query query = new Query();
            Criteria criteria = new Criteria();
            criteria.and(CityTrans.Attributes.COMMERCE_CITY_CODE).is(commerceCode);
            query.addCriteria(criteria);
            CityTrans city = mongoTemplate.findOne(query, CityTrans.class);
            if (city != null) {
                mdmCityCode = city.getMdmCityCode();
                cacheUtils.hsetWithMyExpireTime(CacheKeyConstants.COMMERCE_CITY_CODE_TO_MDM_CODE, commerceCode,
                        mdmCityCode, CacheKeyConstants.CITY_EXPIRE_TIME);
            }
        }
        return mdmCityCode;
    }

    @Override
    public String getMdmCityCodeFromPosCityCode(String posCityCode) {

        String mdmCityCode = cacheUtils.hget(CacheKeyConstants.POS_CITY_CODE_TO_MDM_CODE, posCityCode, String.class);
        if (StringUtils.isBlank(mdmCityCode)) {
            Query query = new Query();
            Criteria criteria = new Criteria();
            criteria.and(CityTrans.Attributes.POS_CITY_CODE).is(posCityCode);
            query.addCriteria(criteria);
            CityTrans city = mongoTemplate.findOne(query, CityTrans.class);
            if (city != null) {
                mdmCityCode = city.getMdmCityCode();
                cacheUtils.hsetWithMyExpireTime(CacheKeyConstants.POS_CITY_CODE_TO_MDM_CODE, posCityCode, mdmCityCode,
                        CacheKeyConstants.CITY_EXPIRE_TIME);
            }
        }
        return mdmCityCode;
    }

    @Override
    public List<CityInfoDto> queryCityInfoList() {
        String cityStr = cacheUtils.get(CacheKeyConstants.EXCHANGE_ALL_CITY);
        List<CityInfoDto> result;
        if (StringUtils.isEmpty(cityStr)) {
            result = new ArrayList<CityInfoDto>();
            Query query = new Query();
            query.with(new Sort(new Order(Direction.ASC, "mdmCityCode")));
            List<CityTrans> cityList = mongoTemplate.findAll(CityTrans.class);
            CityInfoDto cityInfo;
            for (CityTrans per : cityList) {
                cityInfo = new CityInfoDto();
                cityInfo.setCityCode(
                        StringUtils.leftPad(per.getMdmCityCode(), ExchangeConstants.COUPON_CENTER_CITY_LEN, "0"));
                cityInfo.setCityName(per.getCityName());
                result.add(cityInfo);
            }
            cacheUtils.set(CacheKeyConstants.EXCHANGE_ALL_CITY, new Gson().toJson(result));
            cacheUtils.expire(CacheKeyConstants.EXCHANGE_ALL_CITY, CacheKeyConstants.CITY_EXPIRE_TIME);
        } else {
            result = new Gson().fromJson(cityStr, new TypeToken<List<CityInfoDto>>() {
            }.getType());
        }
        return result;
    }

}
